package cn.itsource.pethome.detp.service.impl;

import cn.itsource.pethome.base.util.PageList;
import cn.itsource.pethome.detp.domian.Department;
import cn.itsource.pethome.detp.mapper.IDepartmentMapper;
import cn.itsource.pethome.detp.query.DepartmentQuery;
import cn.itsource.pethome.detp.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private IDepartmentMapper departmentMapper;
    @Override
    public Department findByDeptId(Long id) {
        return departmentMapper.findByDeptId(id);
    }

    @Override
    public PageList<Department> findAll(DepartmentQuery departmentQuery) {
        PageList<Department> pageList = new PageList<>();
        Long total = departmentMapper.findCount(departmentQuery);
        List<Department> rows = departmentMapper.findAll(departmentQuery);
        pageList.setRows(rows);
        pageList.setTotal(total);
        return pageList;
    }

    @Override
    public void addDept(Department department) {
        departmentMapper.addDept(department);
        this.uptDeptById(department);//抽取
    }

    @Override
    public void uptDeptById(Department department) {
        //如果是顶层
        if(department.getParent_name()==null){
            department.setDirPath("/"+department.getId());
        }else{
            //把你的父亲查询出来
            Department parenDept = departmentMapper.findByDeptId(department.getParent_name().getId());
            //获取父亲的dirpath
            String dirPath = parenDept.getDirPath();
            //把你父亲的写在前面
            department.setDirPath(dirPath+"/"+department.getId());
        }
        departmentMapper.uptDeptById(department);
    }

    @Override
    public void delDeptById(Long id) {
        departmentMapper.delDeptById(id);
    }

    @Transactional
    @Override
    public void batchDelDept(List<Integer> ids) {
        departmentMapper.batchDelDept(ids);
    }

    @Override
    public List<Department> getTree() {
//        1.一次性查询出所有数据，放到内存all
        List<Department> deptAll = departmentMapper.findDeptAll();
        List<Department> firstTree = new ArrayList<>();
        for (Department department : deptAll) {
            if(department.getParent_id() == null){
//          2.从所有数据中找出第一层级firstCourseType我们需要返回的数据
                firstTree.add(department);
            }else{
//          3.只要不是第一层级的，找自己的上级，并将自己放入上级的children中
                for (Department dept1 : deptAll) {
                   if(department.getParent_id() == dept1.getId()){
                       //将自己放入到父亲的children中
                       dept1.getChildren().add(department);
                   }
                }
            }
        }
        return firstTree;
    }

    public List<Department> getTree2(){
        List<Department> deptAll = departmentMapper.findDeptAll();
        //把数据转为map
        Map<Long,Department> deptAllMap = new HashMap<>();
        for (Department department : deptAll) {
            deptAllMap.put(department.getId(),department);
        }
        //找第一层
        List<Department> firstTree = new ArrayList<>();
        for (Department department : deptAll) {
            if(department.getParent_id() == null){
                firstTree.add(department);
            }else{
                //你爹
                Department department1 = deptAllMap.get(department.getParent_id());
                department1.getChildren().add(department);
            }
        }
        return firstTree;
    }

}
